package EmployeeSalaryManagementSystem;

/**
 * 普通员工类 - 按小时计薪
 */
public class RegularEmployee extends Employee {
    private double hourlyRate;
    private double hoursWorked;

    public RegularEmployee(String name, String id, double hourlyRate) {
        super(name, id);
        this.hourlyRate = hourlyRate;
        this.hoursWorked = 0;
    }

    @Override
    public double calculateSalary() {
        try {
            validateWorkHours();
            double salary = hoursWorked * hourlyRate;
            // 加班计算：超过40小时按1.5倍计算
            if (hoursWorked > 40) {
                salary += (hoursWorked - 40) * hourlyRate * 0.5;
            }
            return salary;
        } catch (InvalidWorkHoursException e) {
            System.err.println("计算薪资时出错: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public String generatePaySlip() {
        double salary = calculateSalary();
        StringBuilder paySlip = new StringBuilder();
        paySlip.append("=== 普通员工工资条 ===\n");
        paySlip.append("姓名: ").append(name).append("\n");
        paySlip.append("工号: ").append(id).append("\n");
        paySlip.append("时薪: ¥").append(String.format("%.2f", hourlyRate)).append("\n");
        paySlip.append("工作时长: ").append(hoursWorked).append(" 小时\n");
        paySlip.append("基本工资: ¥").append(String.format("%.2f", hoursWorked * hourlyRate)).append("\n");
        if (hoursWorked > 40) {
            paySlip.append("加班工资: ¥").append(String.format("%.2f", (hoursWorked - 40) * hourlyRate * 0.5)).append("\n");
        }
        paySlip.append("实发工资: ¥").append(String.format("%.2f", salary)).append("\n");
        paySlip.append("=====================");
        return paySlip.toString();
    }

    private void validateWorkHours() throws InvalidWorkHoursException {
        if (hoursWorked < 0) {
            throw new InvalidWorkHoursException("工作时长不能为负数: " + hoursWorked);
        }
        if (hoursWorked > 168) { // 一周最多168小时
            throw new InvalidWorkHoursException("工作时长超过合理范围: " + hoursWorked);
        }
    }

    // Getters and Setters
    public double getHourlyRate() {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public double getHoursWorked() {
        return hoursWorked;
    }

    public void setHoursWorked(double hoursWorked) {
        this.hoursWorked = hoursWorked;
    }
}

